package com.lovi.mysql_;

import com.lovi.utils.JDBCUtils;
import com.lovi.utils.ScannerUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class MyPreparedStatementDML_ {
    public static void main(String[] args) throws SQLException {
        //加载配置获得数据源连接
        Connection connection = JDBCUtils.getConnection();
        String sql = "update user set password = ? where username = ?";
        System.out.println("请输入要修改密码的用户名");
        String username = ScannerUtils.readString(10);
        System.out.println("请输入要修改的密码为");
        String password = ScannerUtils.readString(20);

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, password);
        preparedStatement.setString(2, username);
        int rows = preparedStatement.executeUpdate();
        if(rows>0){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
        //关闭连接
        preparedStatement.close();
        connection.close();
    }
}
